#include <bits/stdc++.h>
using namespace std;

int main(){
    int n;
    int t = 1;
    while(cin >> n, n) {
        vector<queue<int>> qu(n);
        queue<int> q;
        unordered_map<int, int> mp;
        for(int i = 0; i < n; i++) {
            int m;
            cin >> m;
            for(int j = 0; j < m; j++) {
                int x;
                cin >> x;
                mp[x] = i;
            }
        }
        cout << "Scenario #" << t << '\n';
        string s;
        cin >> s;
        while(s != "STOP") {
            if(s == "ENQUEUE") {            
                int x;
                cin >> x;
                int pos = mp[x];
                if(qu[pos].empty()) q.push(pos);
                qu[pos].push(x);
            } else{
                cout << qu[q.front()].front() << '\n';
                qu[q.front()].pop();
                if(qu[q.front()].empty()) {
                    q.pop();
                }
            }
            cin >> s;
        }
        t++;
        cout << '\n';
    }
    return 0;
}